<template>
  <div class="app-container">
    <!-- 输入表单 -->
    <el-form label-width="120px">
      <el-form-item label="俱乐部名称">
        <el-input v-model="clue.name" />
      </el-form-item>
      
      <el-form-item label="所在地">
        <el-select v-model="clue.countryId" clearable placeholder="请选择">
        <el-option
          v-for="item in countryList"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
      </el-form-item>
      
      <el-form-item label="BOSS">
        <el-input v-model="clue.boss"/>
      </el-form-item>
      <el-form-item label="建立时间">
        <!-- <el-input v-model="clue.buildTime"/> -->
        <el-date-picker
          v-model="clue.buildTime"
          align="right"
          type="date"
          placeholder="选择日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <!-- 俱乐部图片 -->
      <el-form-item label="俱乐部图片">
        <el-upload
          :action="BASE_API + '/clue/upload'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          class="avatar-uploader">
          <img v-if="clue.photo" :src="clue.photo" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="saveOrUpdate()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>


<script>

import clueApi from "@/api/clue/clue";
import countryApi from "@/api/country/country";
export default {
    data() {
        return {
            BASE_API: 'http://localhost:8888',
            clue:{},
            countryList: [],
            pickerOptions: {
              disabledDate(time) {
                return time.getTime() > Date.now();
              },
              shortcuts: [{
                text: '今天',
                onClick(picker) {
                  picker.$emit('pick', new Date());
                }
              }, {
                text: '昨天',
                onClick(picker) {
                  const date = new Date();
                  date.setTime(date.getTime() - 3600 * 1000 * 24);
                  picker.$emit('pick', date);
                }
              }, {
                text: '一周前',
                onClick(picker) {
                  const date = new Date();
                  date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                  picker.$emit('pick', date);
                }
              }]
            },
        }
    },
    created() {
      countryApi.getCountryList()
        .then((response) => {
          this.countryList = response.data
        })
      if(this.$route.params.id){
        const id = this.$route.params.id;
        this.fetchDataById(id)
      }
    },
    methods: {
      fetchDataById(id){
        clueApi.getClueById(id)
        .then(response => {
          this.clue = response.data
        })
      },
      //添加
      save(){
        clueApi.saveClue(this.clue)
        .then((response) => {
            this.$message({
                type: 'success',
                message: '添加成功!'
            });
            //跳转列表页面
            this.$router.push({path:'/clue/list'})
        })
      },
      //修改
      update(){
        clueApi.updateClue(this.clue)
        .then((response) => {
            this.$message({
                type: 'success',
                message: '修改成功!'
            });
            //跳转列表页面
            this.$router.push({path:'/clue/list'})
        })
      },
      saveOrUpdate(){
        if(this.clue.id){
          this.update()
        }else{
          this.save()
        }
      },
      // 上传成功回调
      handleAvatarSuccess(res, file) {
        // console.log(res)
        if (res.code==20000) {
          // console.log(res)
          this.clue.photo = res.data
          // 强制重新渲染
          this.$forceUpdate()
        } else {
          this.$message.error('上传失败 （非0）')
        }
      },

      // 错误处理
      handleAvatarError() {
        console.log('error')
        this.$message.error('上传失败（http失败）')
      },

      // 上传校验
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 1024 < 2

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!')
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return isJPG && isLt2M
      }
    },
    
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>